﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SortOf
{
    public struct pqItem
    {
        public int priority;
        public object pObject;
    }

    public class PriorityQueue : Queue
    {


        //Constructor, nothing has to be called
        public PriorityQueue()
        {
            //More info on Queue.cs constructor
        }

        //This method overrides the normal Dequeue to dequeue the object with the highest priority.
        public override object DeQueue()
        {
            object[] items;
            int min, minindex;
            items = this.ToArray();

            pqItem item = (pqItem)items[0];
            min = item.priority;
            minindex = 0;

            //Find the item with the highest priority
            for (int i = 1; i < items.Length; i++)
            {
                if (((pqItem)items[i]).priority > min)
                {
                    min = ((pqItem)items[i]).priority;
                    minindex = i;
                }
            }

            //Clear the queue and rebuild it with all the remaining objects that aren't removed.
            this.ClearQueue();
            for (int i = 0; i < items.Length; i++)
            {
                if (i != minindex && ((pqItem)items[i]).pObject != null)
                {
                    this.EnQueue(items[i]);
                }
            }
            return items[minindex];
        }
    }
}
